package com.zys.el.user.util.html;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


@Component
public class lanQiao {

    public static Map<String, Date> getNew() throws IOException {
        String apiUrl = "https://www.guoxinlanqiao.com/api/news/find";
        String status = "1";
        String project = "dasai";
        String progid = "20";
        String pageno = "1";
        String pagesize = "10";

        // 拼接URL和查询参数
        String query = String.format("?status=%s&project=%s&progid=%s&pageno=%s&pagesize=%s",
                status, project, progid, pageno, pagesize);
        URL url = new URL(apiUrl + query);

        // 创建HTTP连接
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");

        // 读取API响应
        Scanner scanner = new Scanner(conn.getInputStream());
        StringBuilder sb = new StringBuilder();
        while (scanner.hasNext()) {
            sb.append(scanner.nextLine());
        }
        String apiResponse = sb.toString();

//        System.out.println(apiResponse);
        // 处理API响应，例如使用JSON库将响应解析为JSON对象
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(apiResponse);
        JsonNode datalist = jsonNode.get("datalist");
        Map<String, Date> re = new HashMap<>();
        for (JsonNode node : datalist) {
            String creatTime = String.valueOf(node.get("creatTime")).trim();
            String synopsis = node.get("synopsis").toString();
//            System.out.println(creatTime +   " : " + synopsis);
            creatTime = creatTime.replace("\"", "");
            LocalDateTime dateTime = LocalDateTime.parse(creatTime);

            ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.systemDefault());
            Instant instant2 = zonedDateTime.toInstant();
            Date date2 = Date.from(instant2);

            re.put(synopsis, date2);

        }
//        System.out.println(re);
        return  re;
    }
    public static void main(String[] args) throws IOException, ParseException {
//        String apiUrl = "https://www.guoxinlanqiao.com/api/news/find";
//        String status = "1";
//        String project = "dasai";
//        String progid = "20";
//        String pageno = "1";
//        String pagesize = "10";
//
//        // 拼接URL和查询参数
//        String query = String.format("?status=%s&project=%s&progid=%s&pageno=%s&pagesize=%s",
//                status, project, progid, pageno, pagesize);
//        URL url = new URL(apiUrl + query);
//
//        // 创建HTTP连接
//        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//        conn.setRequestMethod("GET");
//
//        // 读取API响应
//        Scanner scanner = new Scanner(conn.getInputStream());
//        StringBuilder sb = new StringBuilder();
//        while (scanner.hasNext()) {
//            sb.append(scanner.nextLine());
//        }
//        String apiResponse = sb.toString();
//
////        System.out.println(apiResponse);
//        // 处理API响应，例如使用JSON库将响应解析为JSON对象
//        ObjectMapper objectMapper = new ObjectMapper();
//        JsonNode jsonNode = objectMapper.readTree(apiResponse);
//        JsonNode datalist = jsonNode.get("datalist");
//        Map<String, Date> re = new HashMap<>();
//        for (JsonNode node : datalist) {
//            String creatTime = String.valueOf(node.get("creatTime")).trim();
//            String synopsis = node.get("synopsis").toString();
//            System.out.println(creatTime +   " : " + synopsis);
//            creatTime = creatTime.replace("\"", "");
//            LocalDateTime dateTime = LocalDateTime.parse(creatTime);
//
//            ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.systemDefault());
//            Instant instant2 = zonedDateTime.toInstant();
//            Date date2 = Date.from(instant2);
//
//            re.put(creatTime, date2);
//
//        }
//        System.out.println(re);
        Map<String, Date> aNew = lanQiao.getNew();
//        System.out.println(aNew);
    }

}
